#!/usr/bin/env python
# coding: utf-8
from selenium import webdriver
from config.useragent import user_agent_list
import random  # 取随机数
import sys
user_agent_len = len(user_agent_list) - 10


class Proxy(object):
    def __init__(self, ip):
        self.proxy_ip = 'http://%s' % ip
        # self.driver = self.getdriver()
        # self.getpage(self.driver)

    def getdriver(self):
        options = webdriver.ChromeOptions()
        # 设置代理
        desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()
        desired_capabilities['proxy'] = {
            "httpProxy": self.proxy_ip,
            # "ftpProxy": self.proxy_ip,  # 代理ip是否支持这个协议
            # "sslProxy": self.proxy_ip,  # 代理ip是否支持这个协议
            "noProxy": None,
            "proxyType": "MANUAL",
            "class": "org.openqa.selenium.Proxy",
            "autodetect": False
        }
        # 使用无头模式
        options.add_argument('--headless')
        # 谷歌文档提到需要加上这个属性来规避bug
        options.add_argument('--disable-gpu')
        # 禁止图片
        # prefs = {"profile.managed_default_content_settings.images": 2}
        # options.add_experimental_option("prefs", prefs)
        # 禁止图片 的另外一种发法  不能获取图片的任何信息 程序死循环
        options.add_argument('blink-settings=imagesEnabled=false')

        options.add_argument('user-agent=%s' % user_agent_list[random.randint(0, user_agent_len)])
        if sys.platform == 'darwin':
            print('Mac Os X')
            driver = webdriver.Chrome(chrome_options=options, desired_capabilities=desired_capabilities)
        elif sys.platform == 'win32':
            print('Window')
            driver = webdriver.Chrome(executable_path='D:\driver\chromedriver', chrome_options=options, desired_capabilities=desired_capabilities)

        # driver = webdriver.Chrome(executable_path='D:\driver\chromedriver',chrome_options=options, desired_capabilities=desired_capabilities)
        return driver

    def getpage(self, driver):
        # 打开目标网站
        driver.get("https://www.baidu.com")


